package web

import (
	"gitee.com/slion/account/core/accounts"
	"gitee.com/slion/account/services"
	"gitee.com/slion/helper"
	"gitee.com/slion/infra/base"
	"github.com/kataras/iris"
)

// web api

// 初始化方法里,注册AccountApi
func init() {
	base.RegisterApi(new(AccountApi))
}

// account模块API结构体
type AccountApi struct {
}

// AccountApi实现了Initializer接口
func(api *AccountApi) Init(){
	// 注册路由
	routeGroup := base.IrisApplication().Party("/v1/account")
	routeGroup.Post("/create", Create)
	routeGroup.Post("/transfer", Transfer)
	routeGroup.Get("/envelope", GetEnvelopesAccountByUserId)
	routeGroup.Get("", getAccount)
}

// 账户创建
// path: /v1/account/create
// method: POST
func Create(ctx iris.Context){

	dto := services.AccountCreateDTO{}
	response := helper.Response{
		Code:    helper.ResponseCodeOk,
	}
	// 获取请求参数
	err := ctx.ReadJSON(&dto)
	if err != nil {
		response.Code = helper.ResponseCodeRequestParamsError
		response.Message = err.Error()
		ctx.JSON(&response)
		return
	}

	// 获取accountService实例,调用CreateAccount()方法执行账户创建

	// 1. 视频演示
	accountService := services.GetAccountService()
	accountInfo, err := accountService.CreateAccount(dto)

	// 2. 自己思考
	// accountInfo, err := accounts.AccountService.CreateAccount(dto)

	if err != nil{
		response.Code = helper.ResponseCodeInnerServerError
		response.Message = err.Error()
		ctx.JSON(&response)
		return
	}
	response.Data = accountInfo
	ctx.JSON(&response)
}


// 转账
// path: /v1/account/transfer
// method: POST
func Transfer(ctx iris.Context){

	dto := services.AccountTransferDTO{}
	response := helper.Response{
		Code:    helper.ResponseCodeOk,
	}
	// 获取请求参数
	err := ctx.ReadJSON(&dto)
	if err != nil {
		response.Code = helper.ResponseCodeRequestParamsError
		response.Message = err.Error()
		ctx.JSON(&response)
		return
	}

	// 转账
	status, err := accounts.AccountService.Transfer(dto)
	if err != nil{
		response.Code = helper.ResponseCodeInnerServerError
		response.Message = err.Error()
		ctx.JSON(&response)
		return
	}
	if status != services.TransferStatusSuccess {
		response.Code = helper.ResponseCodeBusinessError
		response.Message = "转账失败"
		ctx.JSON(&response)
		return
	}
	response.Data = status
	ctx.JSON(&response)
}

// 根据userId查询红包账户
// path: /v1/account/envelope
// method: GET
func GetEnvelopesAccountByUserId(ctx iris.Context) {

	userId := ctx.URLParam("userId")
	response := helper.Response{
		Code:    helper.ResponseCodeOk,
	}
	if userId == "" {
		response.Code = helper.ResponseCodeRequestParamsError
		response.Message = "用户ID不能为空"
		ctx.JSON(&response)
		return
	}
	account := accounts.AccountService.GetEnvelopesAccountByUserId(userId)
	response.Data = account
	ctx.JSON(&response)
}

// 根据accountNo查询账户信息
// path: /v1/account
// method: GET
func getAccount(ctx iris.Context) {

	accountNo := ctx.URLParam("accountNo")
	response := helper.Response{
		Code:    helper.ResponseCodeOk,
	}
	if accountNo == "" {
		response.Code = helper.ResponseCodeRequestParamsError
		response.Message = "账户编号不能为空"
		ctx.JSON(&response)
		return
	}

	account := accounts.AccountService.GetAccount(accountNo)
	response.Data = account
	ctx.JSON(&response)
}